//
//  SearchViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/28.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "SearchViewController.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#import "SearchRangeViewController.h"
#import "DBSphereView.h"
#import "SearchRangeViewController.h"

@interface SearchViewController ()<UISearchBarDelegate>

@property (nonatomic, retain) DBSphereView *dbs;

@end

@implementation SearchViewController

- (void)dealloc
{
    [_search release];
    [_dbs release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createSearchBar];
    [self startConnection];
}

#pragma mark -
#pragma mark 创建搜索框
- (void)createSearchBar
{
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, 30)];
    self.search.placeholder = @"请输入动漫名称";
    self.search.autocapitalizationType = UITextAutocorrectionTypeYes;
    self.search.delegate = self;
    [self.view addSubview:self.search];
}



#pragma mark -
#pragma mark 网络请求
- (void)startConnection
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = @"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=hotword";
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        [self createView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark -
#pragma mark 创建大圆球
- (void)createView
{
    self.dbs = [[DBSphereView alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 320)];
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < 50; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 70)];
        [btn setTitle:[self.tableArray objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [array addObject:btn];
        [self.dbs addSubview:btn];
        
    }
    [self.dbs setCloudTags:array];
    [self.view addSubview:self.dbs];
}

- (void)btnClick:(UIButton *)button
{
    [self.dbs timerStop];
    NSLog(@"button = %@", button.currentTitle);
    [UIView animateWithDuration:0.1 animations:^{
        button.transform = CGAffineTransformMakeScale(2.0, 2.0);
        SearchRangeViewController *range = [[SearchRangeViewController alloc] init];
        range.searchText = button.currentTitle;
        [self.navigationController pushViewController:range animated:YES];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            button.transform = CGAffineTransformMakeScale(1.0, 1.0);
        } completion:^(BOOL finished) {
            [self.dbs timerStart];
        }];
    }];
}


#pragma mark -
#pragma mark searchBar协议

//点击搜索按钮之后
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"将要进行搜索的模糊字符串是 %@", self.search.text);
    SearchRangeViewController *range = [[SearchRangeViewController alloc] init];
//    self.search.text = range.searchText;
    range.searchText = self.search.text;
    [self.navigationController pushViewController:range animated:YES];
    [range release];
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.search resignFirstResponder];
}











@end
